package org.wanda.pv.service.basic.impl;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.basic.Point;
import org.wanda.pv.model.basic.PointClass;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.basic.PointClassService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiuXuyang
 * Created by lxy on 2018/5/8.
 */

@Service
public class PointClassServiceImpl extends BaseServiceImpl<PointClass> implements PointClassService {
    @Autowired
    public PointClassServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Result add(PointClass pointClass) {
        try {
            if (pointClass != null && pointClass.getName() != null) {
                boolean distinct = this.distinct(pointClass.getName(), pointClass.getClass());
                if (!distinct) return Result.error("名称重复,添加失败!", "名称重复,添加失败!");
                pointClass = dao().insert(pointClass);
                return Result.success("success", pointClass);
            } else {
                return Result.error("数据为空,添加失败!", "数据为空,添加失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public Result del(Long id) {
        try {
            if(!isOk(PointClass.class,id)){
                return Result.error("数据不存在或已删除！","数据不存在或已删除！");
            }
            Map<String, Object> hasRefer = checkHasRefer(id);
            if ((Boolean) hasRefer.get("hasRefer"))
                return Result.error((String) hasRefer.get("msg"), (String) hasRefer.get("msg"));
            PointClass pointClass = dao().fetch(PointClass.class, id);
            pointClass.setDelFlag(true);
            this.vDelete(id);
            return Result.success("success", "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    @Override
    public Result updatePointClass(PointClass pointClass) {
        try {
            PointClass old = dao().fetch(PointClass.class, pointClass.getId());
            if (!old.getName().equals(pointClass.getName())) {
                boolean distinct = this.distinct(pointClass.getName(), pointClass.getClass());
                if (!distinct) return Result.error("名称重复,添加失败!", "名称重复,添加失败!");
            }
            pointClass.setDelFlag(false);
            dao().updateIgnoreNull(pointClass);
            return Result.success("success", "操作成功!");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    private Map<String, Object> checkHasRefer(Long id) {
        Map<String, Object> result = new HashMap<>();
        PointClass pointClass = dao().fetch(PointClass.class, id);
        dao().fetchLinks(pointClass, "points", Cnd.where("delFlag", "=", "0"));
        List<Point> questions = pointClass.getPoints();
        if (questions != null && questions.size() > 0) {
            result.put("hasRefer", true);
            result.put("msg", "有题目引用此数据,删除失败!");
            return result;
        }
        result.put("hasRefer", false);
        return result;
    }
}
